package com.colt.contentcenter.controller.share;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpStatus;
import com.colt.contentcenter.aop.auth.CheckLogin;
import com.colt.contentcenter.domain.dto.content.ShareDTO;
import com.colt.contentcenter.domain.dto.user.UserDTO;
import com.colt.contentcenter.domain.entity.content.Share;
import com.colt.contentcenter.feighclient.TestUserCenteFeighrClient;
import com.colt.contentcenter.service.share.ShareService;
import com.colt.contentcenter.util.JwtOperator;
import com.colt.contentcenter.util.response.BaseResponse;
import com.colt.contentcenter.util.response.CodeEnum;
import com.colt.contentcenter.util.response.ResponseData;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @program: content-center
 * @description: shareController
 * @author: colt
 * @create: 2020-05-02 10:48
 **/
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/shares")
@RestController
public class ShareController {
    private final ShareService shareService;
    private final RestTemplate restTemplate;
    private final DiscoveryClient discoveryClient;
    private final TestUserCenteFeighrClient testUserCenteFeighrClient;
    private final JwtOperator jwtOperator;
    @GetMapping("/getShareInfo")
    public BaseResponse getShareInfo(){
        List<Share> shareAllInfo = shareService.getShareAllInfo();
        if(CollectionUtil.isEmpty(shareAllInfo)){
            BaseResponse response = BaseResponse.out(CodeEnum.FAIL);
            response.setMsg("未查询到信息~");
            response.setCode(HttpStatus.HTTP_INTERNAL_ERROR);
            return response;
        }
        BaseResponse out = ResponseData.out(CodeEnum.SUCCESS,shareAllInfo);
        out.setMsg("查询成功~");
        out.setCode(HttpStatus.HTTP_ACCEPTED);
        return out;

    };

    @CheckLogin
    @GetMapping("/exchange/{shareId}")
    public Share exchangeById(@PathVariable("shareId") Integer shareId){
//        获取分享id
//        return this.shareService.exchangeByIdFeigh(id);
        return this.shareService.exchangeByIdMQ(shareId);
    }
   /* @GetMapping("/rabbit")
    public String testRabbit(){
        this.shareService.testRabbit();
        return "222";
    }*/

    @GetMapping("/queryPage")
    public PageInfo<Share> query(
            @RequestParam(required = false) String title,
            @RequestParam(required = false,defaultValue = "1")Integer pageNo,
            @RequestParam(required = false,defaultValue = "10")Integer pageSize,
            @RequestHeader("X-Token")String token){
        /**
        * @Description: 查询分享列表
        * @Param: [title, pageNo, pageSize]
        * @return: com.github.pagehelper.PageInfo<com.colt.contentcenter.domain.entity.content.Share>
        * @Author: colt
        * @Date: 2020/11/3
        */
//        注意点：pageSize做控制
        if (pageSize>100){
            pageSize = 100;
        }
        Integer userId = null;
        if (StringUtils.isNotBlank(token)){
            userId = null;
            Claims claims = jwtOperator.getClaimsFromToken(token);
            userId = (Integer) claims.get("id");
        }

        return this.shareService.queryPage(title,userId,pageNo,pageSize);
    }

    @GetMapping("gen-token")
    public String genTOken(){
        Map<String,Object> userInfo = new HashMap<>();
        userInfo.put("id",1);
        userInfo.put("wxNickName","colka");
        userInfo.put("role","admin");
        return this.jwtOperator.generateToken(userInfo);

    }

    @CheckLogin
    @GetMapping("/{id}")
    public ShareDTO findById(@PathVariable("id") Integer id
                             //,@RequestHeader("X-Token") String token  //会接收header里的token
                             ){
        return shareService.findById(id);
    }
    @CheckLogin
    @GetMapping("/query")
    public ShareDTO query(@SpringQueryMap UserDTO userDTO){
        UserDTO dto = this.testUserCenteFeighrClient.query(userDTO);
        ShareDTO shareDTO = new ShareDTO();
        shareDTO.setWxId(dto.getWxId());
        shareDTO.setWxNickname(dto.getWxNickname());
        return shareDTO;
    }
    @CheckLogin
    @GetMapping("/byExam/{share}")
    public Share findByExam(@PathVariable("share") Share share){
        return null;
    }
    @GetMapping("/allShare")
    public BaseResponse getAllShare(){
        List<Share> allInfo = shareService.getShareAllInfo();
        if (CollectionUtil.isEmpty(allInfo)){
            BaseResponse out = BaseResponse.out(CodeEnum.FAIL);
            out.setCode(HttpStatus.HTTP_BAD_METHOD);
            out.setMsg("未查询到信息~");
            return out;
        }
        BaseResponse out = ResponseData.out(CodeEnum.SUCCESS,allInfo);
        out.setMsg("查询成功~");
        out.setCode(HttpStatus.HTTP_ACCEPTED);
        return out;
    }
}
